home *** CD-ROM | disk | FTP | other *** search
/ Amiga Mag HDD Backup / Amiga Mag HDD Backup.zip / Amiga Mag HDD Backup / Alexander.img.bin / Alexander.img / 315 2 files Archive.sit / Any plain-text files / ? Any plain-text file 103 < prev    next >
Text File  |  1995-01-03  |  11KB  |  48 lines

  1.  
  2. Part 2 - Power Play
  3.  
  4.      In the previous article I discussed general assembly language concepts. Now let's get specific and relate these ideas to the Amiga. The Amiga has several registers that store information, in fact, 16 of them; they are each 32-bit registers, or one long word. There are eight data registers, D0 to D7, and eight address registers, A0 to A7. Data registers are used mainly for math and Boolean operations and will handle bytes, words or long words. The address registers usually hold addresses or pointers to a location; A7 is also known as the stack pointer, or SP. Many instructions use the SP without you knowing it so it is a good idea to initially save the address in the SP and then forget about using it. Most math operations can be done using address registers but only with words or long words.
  5. FLAGS
  6.      The other major register we'll use is the status register (SR) or, more accurately, it's lower eight bits, the control code register (CCR). Most commands and any operation using data registers affect the CCR - if the result is a positive or negative number, resulted in zero, etc. Different bits (called flags) within the CCR get set or cleared after most operations and other commands can be used to check these flags and act accordingly.
  7.      Assembly language uses a series of short word commands that the assembler converts to binary numbers for the computer. It's this direct conversion to numbers without having to read a Basic program, convert it to tokens and then convert them to computer oprations that makes machine language so fast. The assembly commands are words or abbreviations; we'll learn a few of them in each article and you might want to keep a list of them in a notebook adding to it as we go along.
  8. COMMAND LINES
  9.      Lines of assembly commands may contain three parts - a label, the command and a comment. The label is just a name you give to an address for easy reference. Most addresses need to be at an even location and divisible by 4; the assembler has built in code that will force this for you. Labels are optional in many cases but are required if other parts of the program refer to them. Try to make your labels as descriptive as possible.
  10.      The command is the heart of the program. It's always indented at least one space (I usually tab it). The command word may include .B, .W or .L to indicate it is for a byte, word or long word. This may be followed by another space or tab and one or two operands (most often numbers or registers). If there are two operands the first is called the source and the second is the destination; usually the source is moved to, added to, subtracted from, etc. the destination. If there is only one operand it is considered to be the destination. As you'll soon see, some commands have different formats depending on what you use for the source and destination.
  11.      The last field, while not used by the assembler, is the comment field, again spaced or tabbed and usually prefixed with a semi-colon. I think it's always a good idea to add as many comments as possible; two months after you've written a program it will be hard to remember what all those code lines meant. And it's sure a lot easier for someone else to understand your program if it's well commented. The program you write is the source code and is usually saved with .ASM after the file name; the code generated by the assembler is the executable program and generally has no extension; some compilers, however, may add the extension .EXE after this program.
  12. SOME COMMANDS
  13.      The most frequently used command is some variation of MOVE. MOVE is used to put a numerical value or the contents of one register into another register. This can be done at the .B, .W or .L level. Numbers are prefixed with a # if they are decimal, #$ if they are HEX and #% if they are binary. MOVEQ is used to put an immediate signed eight-bit number (-128 to +127) only into a data register; the rest of the register is filled with the number's sign (0 or 1). MOVEA (.W or .L) moves the source to only an address register; this command will not affect any of the flags.
  14.      Closely associated with MOVE is the command LEA which loads an effective long word address into an address register; again, the flags will not be affected. If you wanted to put the address of the ARRAY1 location into register A5 you would use LEA ARRAY1,A5. This is the same as MOVEA.L #ARRAY1,A5 but using LEA results in shorter and quicker code. LEA can also be used to increase or decrease an address such as LEA 10(A5),A5 which would increase the address in A5 by 10; the # in front of the 10 is understood and omitted. Any register with parentheses around it means "the contents of".
  15.      Addition and subtraction can be done on either type of register using ADD or SUB. Variations of these commands are - ADDA (the destination must be an address register), ADDI (the source must be an immediate numerical value) and ADDQ (the source must be an immediate value between 1 and 8). The same variations apply to SUB. The two multiplication commands are MULS (multiply signed values) and MULU (multiply unsigned values). Both multiply a 16-bit value times a 16-bit value with a 32-bit result in the destination data register. The two division instructions are DIVS (divide signed values) and DIVU (divide unsigned values). Both divide a 32-bit value (destination) by a 16-bit divisor (source); the result is stored in the lower 16 bits of the destination data register and the remainder in the upper 16 bits.
  16. BRANCHING
  17.      So far our commands have made the computer nothing more than a glorified adding machine, but with a branching capability, the computer begins to come alive. Since most operations, especially those using data registers, affect the CCR we can have the program branch to different locations if a result is 0 or not, positive, negative, or equal to another value. All of these variations are some form of branches. Branching can send the computer to a location plus or minus 32,768 bytes away from the branch; short branches (.S) can move plus or minus 127 bytes away. The three types of branches are -
  18.   SIGNED:
  19.     BGE  Branch if Greater or Equal
  20.     BGT  Branch if Greater Than
  21.     BLE  Branch if Less than or Equal
  22.     BLT  Branch if Less Than
  23.     BNE  Branch if Not Equal
  24.   UNSIGNED:
  25.     BHI  Branch if HIgher
  26.     BHS  Branch if Higher or the Same
  27.     BLO  Branch if LOwer
  28.     BLS  Branch if Lower or the Same
  29.   MISCELLANEOUS:
  30.     BEQ  Branch if EQual
  31.     BCC  Branch if Carry Clear
  32.     BCS  Branch if Carry Set
  33.     BMI  Branch if MInus (negative)
  34.     BPL  Branch if PLus (positive)
  35. Closely associated with branches are the jump commands. JMP will move to any label or address (like Basic's GOTO) and JSR will go to a label or address, execute the routine there and then return to the current location on the program (like Basic's GOSUB...RETURN). JSR is used in all calls to library routines.
  36.      In addition to being able to branch or jump, two additional commands are useful. CMP will compare two operands and set the proper CCR flags so you can branch. The data in both operands remains untouched; only the flags change based on subtracting the source from the destination operand. You can compare numerical values to a register (CMPI) or compare a register to an address register (CMPA). Another command is TST. There is only one operand in this command and you can test an address or register to see it it's 0, plus or minus, and then branch accordingly.
  37. STORAGE
  38.      Memory locations are the addresses of specific storage locations where you give the location a label name, reserve space for it, and maybe, assign it a starting value. Space for a single value is reserved at the end of the program using DC (Define Constant); you can reserve a byte, word, or long word (.B/.W/.L) and give it any starting value. For example, STACK DC.L 0 would reserve a long word space at a memory location called STACK with an initial value of 0. Remember that addresses should start at an even location divisible by four.
  39.      Now let's try assembling and running an actual program. Use ED, or some other type of word processor, to type Listing 1 exactly as is. Remember to indent or tab the commands followed by a space or tab and then the operands. The remarks are optional and are for your benefit. Save this listing as POWER.ASM. Get your assembler out and we'll assemble the listing. Since I'm using PHXASS the following instructions will apply to that assembler but will probably work on most others (A68K, etc.).
  40. WRITING A PROGRAM 
  41.      Let's review the program (Listing 1) and follow the command lines. The first line lets the assembler know that the following lines are source code. The address in the Stack Pointer (A7) is saved in memory location STACK. The number 6 is stored in register D1 as a counter. Amiga address $BFE001 affects, among other things, the power light. When bit1 is set the light is off, and when it's 0 the light is on. If we OR this location with 00000010 (#2) we'll set bit1 not changing any other bit. Then put a 0 in D0 and keep subtracting 1 from it, branching back to label DELAY1 until D0 is again 0; repeat this process for DELAY2.
  42.      Now AND the same memory location with 11111101 (#253); this will force bit1 to 0 and keep the other bits unchanged. Repeat the procedure for the two delays then decrease D1 by 1. If it's not 0 we haven't repeated the entire procedure 6 times so it's back to POWER_OFF. When we have completed 6 blinks, restore the stack pointer to the SP. RTS is a command to return to where you were, in this case probably CLI. The EVEN command is a PHXASS assembler command that forces the address for STACK to be at an even numbered word-aligned location. Finally, END lets the assembler know that this is the end of your program, there is no more data or code.
  43. ASSEMBLY
  44.      Type PHXASS POWER.ASM; the assembler will run for a few seconds then let you know how many lines of code and the number of bytes that are in the assembled program. The file produced is POWER.O and must be linked to any outside or include files, so now type PHXLNK POWER.O. This will rapidly link your program and produce the listing POWER, the executable program. If there are any mistakes in the listing the assembler will tell you the line number and error; it won't tell you if the program will run or crash - only if it's written in an acceptable manner. Run the program POWER and you'll see your power light blink on and off six times.
  45.      Congratulations - you've completed a successful assembly language program. Try changing the delay values and see what difference that makes. If you want to see a listing of the assembled program, type PHXASS POWER.ASM -L, then use MORE or ED to read the listing and see the actual code numbers; try to relate them to the numbers in the source code. You'll notice that PHXASS has optimized your code by 12 bytes. Based on previous information discussed, can you figure out where 10 of these bytes came from and how to improve the source code? Next time I'll discuss the PHXASS assembler in more detail, we'll open some libraries and print their locations.
  46.          
  47.  
  48.     ╣C!"╩w∩Σñ¬^<Çß▐ÇFáZA▐τ`'╝óεoª─MeÜ┤nh┤ldÇB₧!¼/